import axios from "axios";

export const submitCodeToJudge0 = async (
  source_code,
  language_id,
  stdin,
  expected_output,
  cpu_time_limit,
  memory_limit
) => {
  // 第一个请求的配置
  const firstRequestConfig = {
    url: "http://38.12.36.33:2358/submissions",
    method: "POST",
    headers: {
      "content-type": "application/json",
    },
    data: {
      source_code,
      language_id,
      number_of_runs: null,
      stdin,
      expected_output,
      cpu_time_limit,
      cpu_extra_time: null,
      wall_time_limit: null,
      memory_limit,
      stack_limit: null,
      max_processes_and_or_threads: null,
      enable_per_process_and_thread_time_limit: null,
      enable_per_process_and_thread_memory_limit: null,
      max_file_size: null,
      enable_network: null,
    },
  };

  try {
    // 发送第一个请求
    const response = await axios(firstRequestConfig);
    const token = response.data.token;
    console.log("第一个请求成功，获取到的 token:", token);

    // 等待 2 秒
    await new Promise((resolve) => setTimeout(resolve, 2000));

    // 第二个请求的配置
    const secondRequestConfig = {
      url: `http://38.12.36.33:2358/submissions/${token}`,
      method: "GET",
    };

    // 发送第二个请求
    const secondResponse = await axios(secondRequestConfig);
    console.log("第二个请求成功，响应数据:", secondResponse.data);
    return secondResponse.data;
  } catch (error) {
    console.error("请求失败:", error);
    throw error;
  }
};
